package ferp.android.ads;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.amazon.device.ads.*;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.mediation.MediationAdRequest;
import com.google.android.gms.ads.mediation.customevent.CustomEventBanner;
import com.google.android.gms.ads.mediation.customevent.CustomEventBannerListener;
import ferp.core.log.Log;

/**
 * User: igorgok
 * Date: 10/11/12 4:54 PM
 */
public class AmazonCustomEventBanner implements CustomEventBanner
{
  @Override
  public void requestBannerAd(Context context, final CustomEventBannerListener listener, String s, AdSize adSize, MediationAdRequest mediationAdRequest, Bundle bundle)
  {
    Log.debug(Log.TAG, "Amazon custom event received");

    com.amazon.device.ads.AdLayout banner = new com.amazon.device.ads.AdLayout((Activity)context, com.amazon.device.ads.AdSize.SIZE_320x50);

    banner.setTimeout(20000);
    banner.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL));
    banner.setListener(new AdListener()
    {
      @Override
      public void onAdLoaded(Ad ad, AdProperties adProperties)
      {
        Log.debug(Log.TAG, "amazon AD received");
        listener.onAdLoaded((AdLayout)ad);
      }

      @Override
      public void onAdExpanded(Ad ad)
      {
        Log.debug(Log.TAG, "amazon AD expanded");
        listener.onAdClicked();
      }

      @Override
      public void onAdCollapsed(Ad ad)
      {
        Log.debug(Log.TAG, "amazon AD collapsed");
        listener.onAdClosed();
      }

      @Override
      public void onAdFailedToLoad(Ad ad, AdError adError)
      {
        Log.debug(Log.TAG, "amazon AD failed [" + adError.getCode() + "] - " + adError.getMessage());
        listener.onAdFailedToLoad(adError.getCode().ordinal());
      }

      @Override
      public void onAdDismissed(Ad ad)
      {
        Log.debug(Log.TAG, "amazon AD dismissed");
        listener.onAdClosed();
      }
    });

    banner.loadAd(options);
  }

  @Override
  public void onDestroy()
  {
/*
    if (banner != null)
      banner.destroy();

    banner = null;

    Log.debug(Log.TAG, "amazon AD banner destroyed");
*/
  }

  @Override
  public void onPause() {}
  @Override
  public void onResume() {}

  private static final com.amazon.device.ads.AdTargetingOptions options = new com.amazon.device.ads.AdTargetingOptions();
}
